package com.wst.system.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.security.entity.SysRoleMenuEntity;
import com.wst.system.security.entity.SysUserRoleEntity;
import com.wst.system.security.entity.dto.SysMenuEntityTreeDto;
import com.wst.system.security.entity.vo.SysRoleMenuEntityVo;
import com.wst.system.server.mapper.SysRoleMenuMapper;
import com.wst.system.server.service.SysRoleMenuService;
import com.wst.system.server.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:49
 * @since JDK 1.8
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {



    @Override
    public SysRoleMenuEntity getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public IPage<SysRoleMenuEntity> page(Integer pageNum, Integer pageSize,String roleId) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        IPage<SysRoleMenuEntity> page=this.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return page;
    }

    @Override
    public SysRoleMenuEntity getSysRoleMenuEntityRidMid(Serializable rid, Serializable mid) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenuEntity::getMenuId,mid);
        lambdaQueryWrapper.eq(SysRoleMenuEntity::getRoleId,rid);
        SysRoleMenuEntity sysRoleMenuEntity=this.baseMapper.selectOne(lambdaQueryWrapper);
        return sysRoleMenuEntity;
    }

    @Override
    public List<SysRoleMenuEntity> getSysRoleMenuEntityRid(Serializable rid) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenuEntity::getRoleId,rid);
        List<SysRoleMenuEntity> list = this.baseMapper.selectList(lambdaQueryWrapper);
        return list;
    }

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public List<SysRoleMenuEntity> getSysRoleMenuEntityUid(Serializable uid) {
        List<SysUserRoleEntity> list=sysUserRoleService.selectUserRoleEntityByUid(uid);
        if(list==null){
            return Collections.emptyList();
        }
        List<Integer> roleIds=list.stream().map(SysUserRoleEntity::getRoleId).collect(Collectors.toList());
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysRoleMenuEntity::getRoleId,roleIds);
        List<SysRoleMenuEntity> sysRoleMenuEntities=this.baseMapper.selectList(lambdaQueryWrapper);
        return sysRoleMenuEntities;
    }

    @Override
    public List<SysRoleMenuEntity> getByRoleIds(List<Integer> roleIds) {
        LambdaQueryWrapper<SysRoleMenuEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysRoleMenuEntity::getRoleId,roleIds);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void save(SysRoleMenuEntityVo vo) {
        if(!vo.getList().isEmpty()){
            List<SysMenuEntityTreeDto> leafNodes=getLeafNodes(vo.getList());
            List<Integer> ids=leafNodes.stream().map(SysMenuEntityTreeDto::getId).collect(Collectors.toList());
            List<SysRoleMenuEntity> list=this.getSysRoleMenuEntityRid(vo.getRoleId());
            List<Integer> haveIds=new ArrayList<>();
            if(list!=null && list.size()>0){
                for(SysRoleMenuEntity sysRoleMenuEntity:list){
                    if(!ids.contains(sysRoleMenuEntity.getId())){
                        this.removeById(sysRoleMenuEntity.getId());
                    }else {
                        haveIds.add(sysRoleMenuEntity.getId());
                    }
                }
            }
            List<SysRoleMenuEntity> sysRoleMenuEntityList=new ArrayList<>();
            for(SysMenuEntityTreeDto sysMenuEntityTreeDto:leafNodes){
                if(!haveIds.contains(sysMenuEntityTreeDto.getId())){
                    SysRoleMenuEntity sysRoleMenuEntity=new SysRoleMenuEntity();
                    sysRoleMenuEntity.setRoleId(vo.getRoleId());
                    sysRoleMenuEntity.setMenuId(sysMenuEntityTreeDto.getId());
                    sysRoleMenuEntityList.add(sysRoleMenuEntity);
                }
            }
            super.saveBatch(sysRoleMenuEntityList);
        }
    }

    private List<SysMenuEntityTreeDto> getLeafNodes(List<SysMenuEntityTreeDto> nodes) {
        List<SysMenuEntityTreeDto> leafNodes = new ArrayList<>();
        // 检查输入是否为非空列表
        if (nodes == null || nodes.isEmpty()) {
            return leafNodes; // 返回空列表
        }
        // 遍历当前节点列表
        for (SysMenuEntityTreeDto node : nodes) {
            // 如果没有子节点，添加到叶子节点列表中
            if (node.getChildren() == null || node.getChildren().isEmpty()) {
                leafNodes.add(node);
            } else {
                // 否则，递归处理子节点
                leafNodes.addAll(getLeafNodes(node.getChildren()));
            }
        }
        return leafNodes;
    }
}
